﻿package com.wxsoft.business.physic.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.util.common.HanyuPinyinHelper;
import com.wxsoft.util.wx.UUIDUtil;

import com.wxsoft.business.physic.dao.IPsPhysicSupplierDictDao;
import com.wxsoft.business.physic.service.IPsPhysicSupplierDictService;
import com.wxsoft.business.physic.pojo.PsPhysicFactoryDict;
import com.wxsoft.business.physic.pojo.PsPhysicSupplierDict;

@Service("psPhysicSupplierDictService")
public class PsPhysicSupplierDictServiceImpl implements IPsPhysicSupplierDictService  {
	@Resource
	private IPsPhysicSupplierDictDao dao;
	
	
	public long findCount(PsPhysicSupplierDict psPhysicSupplierDict) {
		return dao.findCount(psPhysicSupplierDict);
	}
	
	public List<PsPhysicSupplierDict> findAll(PageHelper page,PsPhysicSupplierDict psPhysicSupplierDict) {
        List<PsPhysicSupplierDict> r = dao.findAll(page,psPhysicSupplierDict);
		return r;
	}
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public String add(PsPhysicSupplierDict psPhysicSupplierDict) {
		psPhysicSupplierDict.setId(UUIDUtil.getUUID());

		check(psPhysicSupplierDict);

		//设置汉语拼音
		psPhysicSupplierDict.setMnemonic(HanyuPinyinHelper.getFirstLettersLo(psPhysicSupplierDict.getSupplier()));
		
		int r = this.dao.insert(psPhysicSupplierDict);
		if(r==1){
			return "操作成功!";
		}
		return "操作失败!";
	}
	
	public String modify(PsPhysicSupplierDict psPhysicSupplierDict) {

		String sCode = psPhysicSupplierDict.getSupplier_number();
		PsPhysicSupplierDict oldForQ = new PsPhysicSupplierDict();
		oldForQ.setSupplier_number(sCode);
		List<PsPhysicSupplierDict> ppdL = dao.getBy(oldForQ);
		if (ppdL != null && ppdL.size() > 0) {
			PsPhysicSupplierDict oldpps = ppdL.get(0);
			//如果修改了 供应商编号  和其它记录 ID 不相等 ， 则会产生重复，报错
			if(!psPhysicSupplierDict.getSupplier_number().equals(oldpps.getSupplier_number())){
				throw new RuntimeException("编号重复，请重新输入！"
						+ psPhysicSupplierDict.getSupplier_number() + psPhysicSupplierDict.getSupplier());
			}
		}
		//设置汉语拼音
		psPhysicSupplierDict.setMnemonic(HanyuPinyinHelper.getFirstLettersLo(psPhysicSupplierDict.getSupplier()));
		
		int r = this.dao.modify(psPhysicSupplierDict);
		if(r==1){
			return "操作成功!";
		}
		return "操作失败!";
	}
	
	public String delete(PsPhysicSupplierDict psPhysicSupplierDict) {
		int r = this.dao.delete(psPhysicSupplierDict);
		if(r==1){
			return "操作成功!";
		}
		return "操作失败!";
	}

	/**
	 * 新增和修改时校验
	 * @Description:TODO
	 * @author: chenliang
	 * @time:2018-3-18 上午11:27:07
	 */
	public void check(PsPhysicSupplierDict psPhysicSupplierDict){

		String sCode = psPhysicSupplierDict.getSupplier_number();
		PsPhysicSupplierDict oldForQ = new PsPhysicSupplierDict();
		oldForQ.setSupplier_number(sCode);
		List<PsPhysicSupplierDict> ppdL = dao.getBy(oldForQ);
		if (ppdL != null && ppdL.size() > 0) {
			throw new RuntimeException("编号重复，请重新输入！"
					+ psPhysicSupplierDict.getSupplier_number() + psPhysicSupplierDict.getSupplier());
		}
	}
}
